package com.bluesky.javawebbrowser.ui;

import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;

import com.bluesky.javawebbrowser.domain.html.HtmlPage;
import com.bluesky.javawebbrowser.domain.http.Server;

public class MyFrame extends JFrame {

	// 3 boxes, main frame
	Box wholeBox;
	Box addressBox;
	Box contentBox;

	// within addressBox
	JTextField urlText;

	// within contentBox
	JTabbedPane tabbedPane;

	// 2 tabs, source and graphics
	JScrollPane scrollSourcePane;
	JScrollPane scrollGrapgicsPane;
	DomTreePanel domPanel;

	JTextPane sourcePane;
	JPanel graphicsPane;

	public MyFrame() {
		setSize(800, 600);

		wholeBox = Box.createVerticalBox();
		addressBox = Box.createHorizontalBox();
		contentBox = Box.createHorizontalBox();

		wholeBox.add(addressBox);
		wholeBox.add(contentBox);

		setContentPane(wholeBox);

		// addressBox
		Dimension addressBoxDimension = new Dimension(800, 30);
		addressBox.setPreferredSize(addressBoxDimension);
		addressBox.setMaximumSize(addressBoxDimension);

		JLabel urlLabel = new JLabel("URL:");
		urlText = new JTextField();
		urlText.setText("http://www.google.com");

		JButton goButton = new JButton("Go");
		goButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton1ActionPerformed(evt);
			}
		});

		addressBox.add(urlLabel);
		addressBox.add(urlText);
		addressBox.add(goButton);

		// contentBox
		tabbedPane = new JTabbedPane();
		contentBox.add(tabbedPane);

		// sourcePane
		sourcePane = new JTextPane();
		scrollSourcePane = new JScrollPane(sourcePane);

		tabbedPane.addTab("Source", scrollSourcePane);

		// graphicsPane
		graphicsPane = new JPanel();
		graphicsPane.setSize(800, 400);
		scrollGrapgicsPane = new JScrollPane(graphicsPane,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		graphicsPane.setLayout(new FlowLayout());
		tabbedPane.addTab("Graphics", scrollGrapgicsPane);

		// DOM
		domPanel = new DomTreePanel();

		tabbedPane.addTab("DOM", domPanel);

		// Box.createHorizontalBox();
		// contentBox.setVerticalScrollBarPolicy(ScrollPaneConstants.
		// VERTICAL_SCROLLBAR_ALWAYS);
		// contentBox.setSize(300,300);

		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);

	}

	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

		String html = Server.get(urlText.getText());
		sourcePane.setText(html);

		

		// DOM
		HtmlPage page = new HtmlPage(html);
		domPanel.setRootHtmlNode(page.getRoot());

		
		// graphics
		graphicsPane.removeAll();
		//page.paint(graphicsPane);
		
		repaint();
	}
	
	private void paintHtmlUI(){
		
	}

	public static void main(String[] argv) {
		new MyFrame();
	}

}
